package com.taotao.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @program: springcloudAlibaba
 * @ClassName OrderService
 * @description:
 * @author: wangjin
 * @create: 2023-11-07 14:59
 * @Version 1.0
 **/
@RestController
public class OrderService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RotationLoadBalancer loadBalancer;

    @RequestMapping("/orderToMember")
    public String orderToMember() {
        //从注册中心上获取该注册服务列表
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("taotao-member");
        ServiceInstance serviceInstance = serviceInstanceList.get(0);
        URI rpcMemberUrl = serviceInstance.getUri();
        //使用本地rest形式实现rpc调用
        String result = restTemplate.getForObject(rpcMemberUrl + "/getUser", String.class);
        return "订单调用会员获取结果:" + result;
    }

    @RequestMapping("/orderToMember1")
    public String orderToMember1() {
        //从注册中心上获取该注册服务列表
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("taotao-member");
        ServiceInstance rpcMemberUrl=loadBalancer.getSingleAddres(serviceInstanceList);
        String result = restTemplate.getForObject(rpcMemberUrl.getUri() + "/getUser", String.class);
        return "订单调用会员获取结果:" + result;
    }
}
